<?php
namespace app\member\service;
/**
 * 积分处理
 */
class PointsService extends \app\base\service\BaseService {

    /**
     * 操作账户
     * @param $data
     * @return bool
     */
    public function account($data) {
        $data = [
            'user_id' => intval($data['user_id']),
            'points' => intval($data['points']),
            'type' => isset($data['type']) ? intval($data['type']) : 1,
            'deduct' => isset($data['deduct']) ? intval($data['deduct']) : 1,
            'remark' => html_clear($data['remark']),
            'label' => isset($data['label']) ? $data['label'] : 'point',
            'force' => false
        ];
        if(empty($data['user_id'])) {
            return $this->error('无法识别用户!');
        }
        if($data['points'] <= 0) {
            return $this->error('处理积分不正确!');
        }
        if(empty($data['label'])) {
            return $this->error('账户积分类型未指定！');
        }
        $model = target('member/PointsAccount');
        $userInfo = $model->getWhereInfo([
            'A.user_id' => $data['user_id'],
            'A.label' => $data['label']
        ]);
        if(empty($userInfo)) {
            $accountData = [
                'user_id' => $data['user_id'],
                'label' => $data['label']
            ];
            $accountId = target('member/PointsAccount')->add($accountData);
            if(!$accountId) {
                return $this->error('账户创建失败！');
            }
            $userInfo = [
                'account_id' => $accountId,
                'user_id' => $data['user_id'],
                'label' => $data['label'],
                'points' => 0,
                'charge' => 0,
                'spend' => 0
            ];
        }
        //实际操作
        if($data['deduct']) {
            if(!$data['type']){
                if($userInfo['points'] < $data['points'] && !$data['force']){
                    return $this->error('账户积分不足,无法进行扣除!');
                }
            }
            $userData = array();
            if($data['type']){
                $userData['points'] = $userInfo['points'] + $data['points'];
            }else{
                $userData['points'] = $userInfo['points'] - $data['points'];
            }
        }
        if($data['type']){
            $userData['charge'] = $userInfo['charge'] + $data['points'];
        }else{
            $userData['spend'] = $userInfo['spend'] + $data['points'];
        }
        $userData['account_id'] = $userInfo['account_id'];
        if(!$model->edit($userData)){
            return $this->error('账户积分操作失败,请稍候再试!');
        }
        //写入记录
        $logData = array();
        $logData['account_id'] = $userInfo['account_id'];
        $logData['log_no'] = $this->logNo($userInfo['user_id']);
        $logData['time'] = time();
        $logData['points'] = $data['points'];
        $logData['remark'] = $data['remark'];
        $logData['type'] = $data['type'];
        $logId = target('member/PointsLog')->add($logData);
        if(!$logId) {
            return $this->error('积分记录失败,请稍候再试!');
        }
        return $this->success($logId);
    }

    /**
     * 获取流水号
     * @param $userId
     * @return string
     */
    public function logNo($userId = ''){
        mt_srand((double) microtime() * 1000000);
        return $userId . date('Ymd') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
    }

    /**
     * 账户记录
     * @param $uid
     * @param $limit
     * @return mixed
     */
    public function accountLog($uid, $limit,  $type = 'point') {
        $where = array();
        $where['C.user_id'] = $uid;
        $where['B.label'] = $type;
        return target('member/PointsLog')->loadList($where, $limit);
    }

    /**
     * 记录数量
     * @param $uid
     * @return mixed
     */
    public function accountLogCount($uid, $type = 'point') {
        $where = array();
        $where['C.user_id'] = $uid;
        $where['B.label'] = $type;
        return target('member/PointsLog')->countList($where);
    }
}
